package com.tess.tboot.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import com.tess.tboot.base.TbootBaseController;
import com.tess.tboot.base.TbootBaseService;
import com.tess.tboot.common.utils.PageUtil;
import com.tess.tboot.common.utils.ResultUtil;
import com.tess.tboot.common.vo.PageVo;
import com.tess.tboot.common.vo.Result;
import com.tess.tboot.entity.Role;
import com.tess.tboot.entity.UserRole;
import com.tess.tboot.serviceimpl.RoleServiceImpl;
import com.tess.tboot.serviceimpl.UserRoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 拥有ROLE_ADMIN角色的用户可以访问
 *
 * @author Exrickx
 */
@Slf4j
@RestController
@Api(description = "角色管理接口")
@RequestMapping("/role")
public class RoleController extends TbootBaseController<Role, String> {

    @Autowired
    private RoleServiceImpl roleService;

    @Autowired
    private UserRoleServiceImpl userRoleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取service
     *
     * @return
     */
    @Override
    public TbootBaseService<Role> getService() {

        return roleService;
    }

    @RequestMapping(value = "/setDefault", method = RequestMethod.POST)
    @ApiOperation(value = "设置或取消默认角色")
    public Result<Object> setDefault(@RequestParam String id,
                                     @RequestParam Boolean isDefault) {

        Role role = roleService.get(id);
        if (role == null) {
            return new ResultUtil<Object>().setErrorMsg("角色不存在");
        }
        role.setDefaultRole(isDefault);
        roleService.update(role);
        return new ResultUtil<Object>().setSuccessMsg("设置成功");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加角色")
    public Result<Role> add(@ModelAttribute Role role) {

        Role r = roleService.save(role);
        return new ResultUtil<Role>().setData(r);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "更新数据")
    public Result<Role> edit(@ModelAttribute Role entity) {

        int r = roleService.update(entity);
        //手动批量删除缓存
        Set<String> keys = redisTemplate.keys("userRole:" + "*");
        redisTemplate.delete(keys);
        Set<String> keysUser = redisTemplate.keys("user:" + "*");
        redisTemplate.delete(keysUser);
        return new ResultUtil<Role>().setData(roleService.get(entity.getId()));
    }

    @RequestMapping(value = "/delAllByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过ids删除")
    public Result<Object> delByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            List<UserRole> list = userRoleService.findByRoleId(id);
            if (list != null && list.size() > 0) {
                return new ResultUtil<Object>().setErrorMsg("删除失败，包含正被使用中的角色");
            }
        }
        for (String id : ids) {
            roleService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getAllByPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<Page<Role>> getByPage(@ModelAttribute Role formObj, @ModelAttribute PageVo pageVo) {

        Page<Role> page = getService().findWithPage(BeanUtil.beanToMap(formObj),PageUtil.initPage(pageVo));
        return new ResultUtil<Page<Role>>().setData(page);
    }

}
